{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "ChatBot - Day 1.ipynb\"",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZY2tOpvsanoH"
      },
      "source": [
        "import random\n",
        "import nltk"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "W-j5FF8ib0rr"
      },
      "source": [
        "BOT_CONFIG = {\n",
        "    'intents': {\n",
        "        'hello': {\n",
        "            'examples': ['Привет!', 'Здарова', 'Хей-хей!!'],\n",
        "            'responses': ['Хай', 'Добрый вечер!', 'Здравствуйте!']\n",
        "        },\n",
        "        'bye': {\n",
        "            'examples': ['Пока', 'Увидимся!', 'Покеда'],\n",
        "            'responses': ['До свидания', 'Прощайте', 'Сайонара!']\n",
        "        }\n",
        "    }\n",
        "}"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5NtkA4HjdTWb"
      },
      "source": [
        "def clean(text):\n",
        "  text = text.lower()\n",
        "  cleaned_text = ''\n",
        "  for ch in text:\n",
        "    if ch in 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя':\n",
        "      cleaned_text = cleaned_text + ch\n",
        "  return cleaned_text\n",
        "\n",
        "def get_intent(text):\n",
        "  for intent in BOT_CONFIG['intents'].keys():\n",
        "    for example in BOT_CONFIG['intents'][intent]['examples']:\n",
        "      w1 = clean(example)\n",
        "      w2 = clean(text)\n",
        "      if nltk.edit_distance(w1, w2) / max(len(w1), len(w2)) < 0.4:\n",
        "        return intent\n",
        "  return 'интент не найден'"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pzbqFazfmsqm"
      },
      "source": [
        "def bot(question):\n",
        "  intent = get_intent(question)\n",
        "  if intent != 'интент не найден':\n",
        "    print(random.choice(BOT_CONFIG['intents'][intent]['responses']))\n",
        "  else:\n",
        "    print(intent)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BdNrbTMfmZGS",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "3f08db60-d99a-42b2-8a9a-875427233b1f"
      },
      "source": [
        "question = ''\n",
        "while question != 'стоп':\n",
        "  question = input()\n",
        "  bot(question)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "что это?\n",
            "интент не найден\n",
            "привет\n",
            "Хай\n",
            "прощай\n",
            "интент не найден\n",
            "стоп\n",
            "интент не найден\n"
          ]
        }
      ]
    }
  ]
}
